using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Runtime.InteropServices;
using System.Windows.Interop;
using System.Windows.Media;

namespace TwitterCommon
{
    /// <summary>
    /// Provides access to Vista Glass native API to extend the glass frame on a WPF application.
    /// </summary>
    /// <remarks>Please visit http://windowssdk.msdn.microsoft.com/en-us/library/ms748975(VS.80).aspx
    /// Please also see the MS Healthcare Demo</remarks>
    public class GlassHelper
    {
        [StructLayout(LayoutKind.Sequential)]
        public struct MARGINS
        {
            public int cxLeftWidth;      // width of left border that retains its size
            public int cxRightWidth;     // width of right border that retains its size
            public int cyTopHeight;      // height of top border that retains its size
            public int cyBottomHeight;   // height of bottom border that retains its size
        };

        [DllImport("DwmApi.dll")]
        public static extern int DwmExtendFrameIntoClientArea(
            IntPtr hwnd,
            ref MARGINS pMarInset);

        public static void ExtendToEntireFrame(Window window)
        {
            IntPtr hwnd = new WindowInteropHelper(window).Handle;

            window.Background = Brushes.Transparent;
            HwndSource.FromHwnd(hwnd).CompositionTarget.BackgroundColor = Colors.Transparent;

            MARGINS margins = new MARGINS();
            margins.cxLeftWidth = -1;
            margins.cxRightWidth = -1;
            margins.cyBottomHeight = -1;
            margins.cyTopHeight = -1;

            DwmExtendFrameIntoClientArea(hwnd, ref margins);
        }

        public static void RemoveFromFrame(Window window)
        {
            IntPtr hwnd = new WindowInteropHelper(window).Handle;

            MARGINS margins = new MARGINS();
            margins.cxLeftWidth = 5;
            margins.cxRightWidth = 5;
            margins.cyBottomHeight = 5;
            margins.cyTopHeight = 5;

            DwmExtendFrameIntoClientArea(hwnd, ref margins);
        }
    }
}
